package org.jeecg.modules.product.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.product.risk.entity.TbBaseCheckItem;
import org.jeecg.modules.product.risk.entity.TbBaseCheckPlan;
import org.jeecg.modules.product.risk.entity.TbBaseCheckRecord;
import org.jeecg.modules.product.risk.entity.TbBaseCheckTask;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckItemMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckPlanMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckRecordMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseCheckTaskMapper;
import org.jeecg.modules.product.risk.service.ITbBaseCheckTaskService;
import org.jeecg.modules.product.riskUnit.entity.TbBaseRiskDevice;
import org.jeecg.modules.product.riskUnit.mapper.TbBaseRiskDeviceMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 专项检查任务表
 * @Author: jeecg-boot
 * @Date:   2024-05-08
 * @Version: V1.0
 */
@Service
@Slf4j
public class TbBaseCheckTaskServiceImpl extends ServiceImpl<TbBaseCheckTaskMapper, TbBaseCheckTask> implements ITbBaseCheckTaskService {
//    @Value("${doubleControl.token}")
//    private String token;
//    @Value("${doubleControl.host}")
//    private String host;
//    @Value("${doubleControl.port}")
//    private int port;
//    @Value("${doubleControl.companyCode}")
//    private String companyCode;
//    @Value("${doubleControl.getCheckTask}")
//    private String getCheckTask;
//    @Value("${doubleControl.aecKey}")
//    private String aecKey;
//    @Value("${doubleControl.aecIv}")
//    private String aecIv;
    @Autowired
    private TbBaseCheckTaskMapper baseCheckTaskMapper;
//    @Autowired
//    private TbEnterpriseMapper enterpriseMapper;
    @Autowired
    private TbBaseRiskDeviceMapper baseRiskDeviceMapper;
    @Autowired
    private TbBaseCheckItemMapper baseCheckItemMapper;
    @Autowired
    private TbBaseCheckRecordMapper baseCheckRecordMapper;
    @Autowired
    private TbBaseCheckPlanMapper baseCheckPlanMapper;

    @Override
    public int taskDecomposition(TbBaseCheckTask baseCheckTask) {
        QueryWrapper<TbBaseCheckItem> queryWrapper = new QueryWrapper<>();
        QueryWrapper<TbBaseRiskDevice> queryWrapperDevice = new QueryWrapper<>();
        /*-------先把任务id去掉---------------------*/
        queryWrapper.eq("task_id",baseCheckTask.getId());
        queryWrapper.eq("deleted","0");

        queryWrapperDevice.eq("deleted" ,"0");
        queryWrapperDevice.in("serious_hazard","seriousHazard01","seriousHazard02","seriousHazard03","seriousHazard04");
        List<TbBaseCheckItem> itemList = baseCheckItemMapper.selectList(queryWrapper);
        int sum = 0;
        List<TbBaseRiskDevice> deviceListList = baseRiskDeviceMapper.selectList(queryWrapperDevice);
        //step1.有几个重大危险源编号就增添对应的任务数量
        if (!deviceListList.isEmpty() && !itemList.isEmpty()){
            for (TbBaseRiskDevice device: deviceListList){
                TbBaseCheckPlan plan = new TbBaseCheckPlan();
                plan.setTaskId(baseCheckTask.getId());
                plan.setName(baseCheckTask.getTaskName());
                plan.setHazardName(device.getName());
                plan.setInspectType(baseCheckTask.getInspectTypeName());
                /*inspectionType01是内部检查,inspectionType02是外部检查*/
                plan.setInspectionType("inspectionType01");
                plan.setTaskStartTime(baseCheckTask.getTaskStartTime());
                plan.setTaskEndTime(baseCheckTask.getTaskEndTime());
                plan.setIsNot("1");//默认使用检查表
                plan.setRegistrationTime(DateUtils.now());
                plan.setStatus("taskStatus02");
                plan.setDeleted(CommonConstant.DEL_FLAG_0.toString());
                baseCheckPlanMapper.insert(plan);

                for (TbBaseCheckItem item : itemList){
                    TbBaseCheckRecord record = new TbBaseCheckRecord();
                    record.setId(UUID.randomUUID().toString());
                    record.setTaskId(baseCheckTask.getId());
                    record.setCheckType(baseCheckTask.getInspectTypeName());
                    record.setCheckItemId(item.getId());
                    record.setCheckName(item.getCheckName());
                    record.setCompanyCode(device.getCompanyCode());
                    record.setHazardCode(device.getHazardCode());
                    record.setCheckContent(item.getCheckContent());
                    record.setCheckWay(item.getCheckWay());
                    record.setCheckBasis(item.getCheckBasis());
                    record.setApplicablePlace(item.getApplicablePlace());
                    record.setDeleted(CommonConstant.DEL_FLAG_0.toString());
                    record.setPlanId(plan.getId());
                    baseCheckRecordMapper.insert(record);
                }
                sum++;
            }
        }
        UpdateWrapper<TbBaseCheckTask> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",baseCheckTask.getId());
        updateWrapper.set("salvaged", "1");
        baseCheckTaskMapper.update(null,updateWrapper);
        return sum;
    }
}
